using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using BowMaster.SceneElements;
using Utilities;

namespace BowMaster.Levels
{
    enum HitPosition { Upper, Central, Lower, Undefined }
    abstract class StaticTargetBase : ALevel
    {
        protected Texture2D texture, previewTexture, pointTexture;//, hitBoxesText;
        protected StaticTarget target;
        protected Rectangle previewRectangle;
        protected int scores;
        protected int lastScore;
        ArrayList<Rectangle> points;
        protected int objective;
        MyButton current_score, objective_score;
        public StaticTargetBase(Game game, int index, String Name, int objective)
            : base(game, index, Name)
        {
            this.objective = objective;
        }

        protected override void LoadContent()
        {
            texture = Game.Content.Load<Texture2D>("Textures/statictarget");
            //hitBoxesText = Game.Content.Load<Texture2D>("Textures/firingArea");
            previewTexture = Game.Content.Load<Texture2D>("Textures/statictargetpreview");
            pointTexture = Game.Content.Load<Texture2D>("Textures/point");
            previewRectangle = new Rectangle(bar.X - 200, 16, 150, 150);
            points = new ArrayList<Rectangle>();
            scores = 0;
            lastScore = 0;
            
            base.LoadContent();
            objective_score = new MyButton(new Rectangle(0, 20, 200, 50), Strings.Objective_Score + objective, textColor, arial14);
            current_score = new MyButton(new Rectangle(0, 60, 200, 50), Strings.Current_Score + scores, textColor, arial14);
        }
        public override void DrawLevelElements()
        {
            base.DrawLevelElements();
            target.Draw(spriteBatch, view, projection);
            
            //target.DrawHitBoxes(spriteBatch, view, projection, hitBoxesText);
        }
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

            spriteBatch.Draw(previewTexture, previewRectangle, Color.White);
            for (int i = 0; i < points.Count; i++)
                spriteBatch.Draw(pointTexture, points[i], Color.Green);

            current_score.Draw(spriteBatch);
            objective_score.Draw(spriteBatch);

            spriteBatch.End();
            
        }
        public override void Update(GameTime gameTime)
        {
            if (!showHelp)
            {
                var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (activeArrow != null)
                {
                    lastScore = scores;
                    HitPosition pos = HitPosition.Undefined;
                    float d = -1;
                    for (int i = 0; i < target.hitBoxes.Count; i++)
                    {
                        if (StaticUtilities.RectContainsVect(target.hitBoxes[i].box, activeArrow.point))
                        {
                            scores += (int)target.hitBoxes[i].value;
                            current_score.text = Strings.Current_Score + scores;
                            current_score.UpdateTextPos();
                            if (scores >= objective)
                                LevelWon();

                            disabledArrows.Add(activeArrow);



                            if (target.hitBoxes[i].box.Y + target.hitBoxes[i].box.Height / 3 > activeArrow.point.Y)
                                pos = HitPosition.Upper;
                            else if (target.hitBoxes[i].box.Y + target.hitBoxes[i].box.Height * 2 / 3 > activeArrow.point.Y)
                                pos = HitPosition.Central;
                            else
                                pos = HitPosition.Lower;
                            //Console.WriteLine(pos);
                            activeArrow = null;
                            showGetBackButton = true;
                            d = i;
                            break;

                        }
                    }
                    if (lastScore != scores && d != -1 && pos != HitPosition.Undefined)
                    {
                        d = d * 50 * previewRectangle.Height / previewTexture.Height;
                        Rectangle p = new Rectangle(0, 0, 10, 10);

                        switch (pos)
                        {
                            case HitPosition.Central:
                                //d -= 5 * previewRectangle.Height / previewTexture.Height;
                                var a = previewRectangle.Center.X - (float)(d / 2);
                                var b = previewRectangle.Center.X + (float)(d / 2);
                                p.X = rnd.Next((int)a, (int)b);
                                a = previewRectangle.Center.Y - (float)(d / 2);
                                b = previewRectangle.Center.Y + (float)(d / 2);
                                p.Y = rnd.Next((int)a, (int)b);
                                break;
                            case HitPosition.Lower:
                                d -= 25 * previewRectangle.Height / previewTexture.Height;
                                var r = MathHelper.ToRadians(-rnd.Next(90)) + Math.PI;
                                var velocity = Vector2.UnitX * (float)Math.Cos(r) + Vector2.UnitY * (float)Math.Sin(r);
                                velocity *= d;
                                p.X = previewRectangle.X + previewRectangle.Width / 2 + (int)velocity.X + p.Width / 2;
                                p.Y = previewRectangle.Y + previewRectangle.Height / 2 + (int)velocity.Y + p.Height / 2;
                                break;
                            case HitPosition.Upper:
                                d -= 25 * previewRectangle.Height / previewTexture.Height;
                                r = MathHelper.ToRadians(rnd.Next(90)) + Math.PI;
                                velocity = Vector2.UnitX * (float)Math.Cos(r) + Vector2.UnitY * (float)Math.Sin(r);
                                velocity *= d;
                                p.X = previewRectangle.X + previewRectangle.Width / 2 + (int)velocity.X + p.Width / 2;
                                p.Y = previewRectangle.Y + previewRectangle.Height / 2 + (int)velocity.Y + p.Height / 2;
                                break;
                            default: return;
                        }

                        points.Add(p);
                    }
                }

            }
            base.Update(gameTime);
        }
    }
}
